package bsc.war.form;

import bsc.ejb.entity.ScoreCard;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.ScoreCardUtils;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author Miguel Angel
 */
public class DeleteScoreCard extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");
            Long idScoreCard = Long.valueOf(request.getParameter("id"));
            ScoreCard scoreCard = delegate.findScoreCard(idScoreCard);
            List<ScoreCard> scoreCardSons = new ArrayList<ScoreCard>();
            List<ScoreCard> scoreCardFathers = new ArrayList<ScoreCard>();

            boolean deleted = ScoreCardUtils.deleteScoreCard(request, response, delegate, scoreCard);
            if(!deleted){
                // Check if the scorecard has sons
                scoreCardSons = delegate.findOtherScoreCards(scoreCard.getIdScoreCard());
                // Check if the scorecard has daddys
                scoreCardFathers = delegate.findOtherScoreCardsRevert(scoreCard.getIdScoreCard());
            }
            request.setAttribute("scoreCardSons", scoreCardSons);
            request.setAttribute("scoreCardFathers", scoreCardFathers);
            request.setAttribute("deleted", deleted);
            //jsp/css/img/icoDesactivadoColor.png

        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ModelAndView("deleteScoreCard");
    }
}